<template>
    <div class="role-user-list">

        <div class="drawer-top">
            <el-button @click="drawerBoxClose">取 消</el-button>
            <el-button type="primary" @click="onDrawerBoxSubData">{{'保 存 数 据' }}</el-button>
        </div>

        <el-table v-loading="loading" :data="listData" stripe border style="width: 98%;margin-left: 1%;margin-top: 15px"
                  :cell-style="rowClass" :header-cell-style="headClass"  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50"></el-table-column>
            <el-table-column prop="userNick" label="用户昵称" width="150" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="loginCode" label="登陆账号" width="150" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="userPhone" label="手机号" width="150" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="isSystem" label="用户类别" width="120" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span v-if="scope.row.isSystem=='1'">系统用户</span>
                    <span v-if="scope.row.isSystem=='2'">普通用户</span>
                </template>
            </el-table-column>
            <el-table-column prop="userGrade" label="用户等级" width="120" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span v-if="scope.row.userGrade=='1'">普通用户</span>
                    <span v-if="scope.row.userGrade=='2'">普通管理员</span>
                    <span v-if="scope.row.userGrade=='3'">超级管理员</span>
                </template>
            </el-table-column>
            <el-table-column prop="userRole" label="用户角色" width="100" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="status" label="状态" width="100" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span v-if="scope.row.status=='1'" style="color: green">正常</span>
                    <span v-if="scope.row.status=='2'" style="color: red">停用</span>
                </template>
            </el-table-column>
        </el-table>




    </div>
</template>

<script>
    import userApi from '@/api/user/userApi'

    export default {
        name: "RoleUserList",
        data() {
            return {
                loading: true,
                listData: [],  //用户数据
                multipleSelection:[],  //选中数据
                roleCode:'',

            }
        },
        methods:{
            drawerBoxClose(){
                this.$emit('drawerBoxClose',false)
            },
            async onDrawerBoxSubData(){
                let userIds = '';
                this.multipleSelection.forEach((item)=>{
                    userIds=userIds.concat(item.id+",")
                })
                let res = await userApi.roleUser(userIds,this.roleCode);
                if (res.code == '200'){
                    this.getUserListAll();
                    this.$message.success('分配成功！')
                }
            },
            async getUserListAll(){
                let res = await userApi.getListAll(this.user)
                if (res.code == '200'){
                    this.listData = res.data
                    this.loading = false;
                }
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            indexMethod(i){
                return i+1
            },
            headClass () {
                return 'text-align: center;background:#eef1f6;'
            },
            rowClass () {
                return 'text-align: center;'
            },
        },
        props:{
            drawerRoleCode:{
                require:true
            }
        },
        watch:{
            drawerRoleCode:{
                immediate: true,
                handler(newVal,oldVal){
                    if (newVal!=''){
                        this.roleCode = newVal
                    }
                }
            }
        },
        created: async function () {
            let res = await userApi.getListAll(this.user)
            if (res.code == '200'){
                this.listData = res.data
                this.loading = false;
            }
        }
    }
</script>

<style scoped>
    .drawer-top{
        margin-left: 10px;
    }

</style>